import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class GradeManagementSystem {
    private List<StudentGrade> grades = new ArrayList<>();
    private Scanner scanner = new Scanner(System.in);

    // 记录学生成绩
    public void recordGrade() {
        System.out.println("请输入学生姓名:");
        String name = scanner.nextLine();
        System.out.println("请输入学生学号:");
        String studentId = scanner.nextLine();
        if (isDuplicateStudentId(studentId)) {
            System.out.println("学号重复，请重新输入！");
            return;
        }
        System.out.println("请输入课程名称:");
        String courseName = scanner.nextLine();
        System.out.println("请输入成绩(0 - 100):");
        int grade = scanner.nextInt();
        scanner.nextLine();
        if (grade < 0 || grade > 100) {
            System.out.println("成绩范围错误，请重新输入！");
            return;
        }
        StudentGrade studentGrade = new StudentGrade(name, studentId, courseName, grade);
        grades.add(studentGrade);
        System.out.println("成绩记录成功！");
    }

    private boolean isDuplicateStudentId(String studentId) {
        for (StudentGrade grade : grades) {
            if (grade.getStudentId().equals(studentId)) {
                return true;
            }
        }
        return false;
    }

    // 查询学生成绩
    public void queryGrade() {
        System.out.println("请选择查询方式: 1. 按学生姓名 2. 按学生学号 3. 按课程名称");
        int choice = scanner.nextInt();
        scanner.nextLine();
        switch (choice) {
            case 1:
                System.out.println("请输入学生姓名:");
                String name = scanner.nextLine();
                queryByCondition("name", name);
                break;
            case 2:
                System.out.println("请输入学生学号:");
                String studentId = scanner.nextLine();
                queryByCondition("studentId", studentId);
                break;
            case 3:
                System.out.println("请输入课程名称:");
                String courseName = scanner.nextLine();
                queryByCondition("courseName", courseName);
                break;
            default:
                System.out.println("无效的查询方式，请重新选择！");
        }
    }

    private void queryByCondition(String type, String value) {
        boolean found = false;
        for (StudentGrade grade : grades) {
            if (("name".equals(type) && grade.getName().equals(value))
                    || ("studentId".equals(type) && grade.getStudentId().equals(value))
                    || ("courseName".equals(type) && grade.getCourseName().equals(value))) {
                System.out.println("姓名: " + grade.getName() + ", 学号: " + grade.getStudentId() + ", 课程名称: " + grade.getCourseName() + ", 成绩: " + grade.getGrade());
                found = true;
            }
        }
        if (!found) {
            System.out.println("未找到符合条件的成绩记录！");
        }
    }

    // 统计成绩
    public void statisticsGrade() {
        System.out.println("请输入要统计的课程名称:");
        String courseName = scanner.nextLine();
        int sum = 0;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        int count = 0;
        for (StudentGrade grade : grades) {
            if (grade.getCourseName().equals(courseName)) {
                sum += grade.getGrade();
                max = Math.max(max, grade.getGrade());
                min = Math.min(min, grade.getGrade());
                count++;
            }
        }
        if (count == 0) {
            System.out.println("未找到该课程的成绩记录！");
        } else {
            double average = (double) sum / count;
            System.out.println("课程 " + courseName + " 的平均分: " + average + ", 最高分: " + max + ", 最低分: " + min);
        }
    }

    // 显示菜单
    public void displayMenu() {
        while (true) {
            System.out.println("学生成绩管理系统菜单:");
            System.out.println("1. 记录学生成绩");
            System.out.println("2. 查询学生成绩");
            System.out.println("3. 统计成绩");
            System.out.println("4. 退出");
            System.out.println("请输入选项序号:");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    recordGrade();
                    break;
                case 2:
                    queryGrade();
                    break;
                case 3:
                    statisticsGrade();
                    break;
                case 4:
                    System.out.println("安全退出程序！");
                    return;
                default:
                    System.out.println("无效的选项，请重新输入！");
            }
        }
    }
}
